package freelinz.smart.gwtex.client.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import freelinz.smart.gwtex.client.ui.SmartKitsAll.ResizedListener;
import freelinz.smart.gwtex.client.ui.SmartKitsAll.SmartKitI;
import freelinz.smart.gwtex.client.ui.SmartKitsAll.SmartKitsI;
import freelinz.smart.gwtex.user.client.DOMEx;




public abstract class AbstractSmartKit implements SmartKitI
{
    private int openClose;
    
    protected ResizedListenerCollection resizedListenerCollection;

    private SmartKitsI parentSmartKits;

    public SmartKitsI getParentSmartKits()
    {
        return parentSmartKits;
    }

    public void setSizeScale(Float width, Float height)
    {
        
    }

    public void setParentSmartKits(SmartKitsI smartKitsI)
    {
        this.parentSmartKits = smartKitsI;
    }

    public int getOpenClose()
    {
        return this.openClose;
    }

    public void setOpenClose(int openClose)
    {
        this.openClose = openClose;
    }

    public void addResizedListener(ResizedListener listener)
    {
        if(resizedListenerCollection == null)
        {
            resizedListenerCollection = new ResizedListenerCollection();
        }
        resizedListenerCollection.add(listener);
    }

    public void fireResized(int width, int height)
    {
        if(resizedListenerCollection != null)
        {
            resizedListenerCollection.fireResized(width, height);
        }
    }

    public void removeResizedListener(ResizedListener listener)
    {
        if(resizedListenerCollection != null)
        {
            resizedListenerCollection.remove(listener);
        }
    }
    
    public void clear()
    {
        if(resizedListenerCollection != null)
        {
            resizedListenerCollection.clear();
        }
    }

    private class ResizedListenerCollection
    {
        /**
         * 
         */
        private List listeners = new ArrayList();

        public void add(ResizedListener listener)
        {
            this.listeners.add(listener);
        }

        public void remove(ResizedListener listener)
        {
            this.listeners.remove(listener);
        }

        public void fireResized(int width, int height)
        {
            for(Iterator it = listeners.iterator(); it.hasNext();)
            {
                ResizedListener listener = (ResizedListener) it.next();
                if(listener != null)
                    listener.resized(width, height);
            }
        }

        public void clear()
        {
            this.listeners.clear();
        }
    }

    public int getHeight()
    {
        return getWidget().getOffsetHeight()+DOMEx.getMarginTopBottom(getWidget().getElement());
    }

    public int getWidth()
    {
        return getWidget().getOffsetWidth()+DOMEx.getMarginLeftRight(getWidget().getElement());
    }

}
